package 直连版本;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Socket_A_B {
        ServerSocket ss;
        BufferedWriter bw_A,bw_B;
        BufferedReader br_A,br_B;
    public void Run() throws IOException {
        ss=new ServerSocket(33333); //建立帮助连接的socket
        Socket s_A = ss.accept();
        Socket s_B = ss.accept();
        bw_A = new BufferedWriter(new OutputStreamWriter(s_A.getOutputStream()));
        bw_B = new BufferedWriter(new OutputStreamWriter(s_B.getOutputStream()));
        br_A = new BufferedReader(new InputStreamReader(s_A.getInputStream()));
        br_B = new BufferedReader(new InputStreamReader(s_B.getInputStream()));
        new Thread(() -> {
            //B->A
            String s;
            try {
                while (true) {
                    if ((s=br_B.readLine()).equals("886"))break;
                    System.out.println("B->A:"+s);
                    bw_A.write(s);
                    bw_A.newLine();
                    bw_A.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            //A->B
            String s;
            try {
                while (true) {
                    if ((s=br_A.readLine()).equals("886"))break;
                    System.out.println("A->B:"+s);
                    bw_B.write(s);
                    bw_B.newLine();
                    bw_B.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public static void main(String[] args) throws IOException {
        new Socket_A_B().Run();
    }
}
